home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 17.7 KB | 773 lines | [TEXT/R*ch] |
- /*
- File: LogWindow.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __LOGWINDOW__
- #include "LOGWINDOW.h"
- #endif
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __FONTS__
- #include "FONTS.h"
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __WINDOWS__
- #include <Windows.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __LIMITS__
- #include <Limits.h>
- #endif
-
- #ifndef __GRAFPORTSAVER__
- #include "GrafPortSaver.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __OBJECTLIST__
- #include "ObjectList.h"
- #endif
-
- #ifndef __STRSTREAM__
- #include "StrStream.h"
- #endif
-
- #ifndef __THREADCLASSES__
- #include "ThreadClasses.h"
- #endif
-
- /***********************************|****************************************/
-
- DeclareList ( TLogWindow, TLogWindowList );
- ImplementList ( TLogWindow, TLogWindowList , true );
-
- /***********************************|****************************************/
-
- TLogWindowList gLogWindowList;
-
- /***********************************|****************************************/
-
- static VHSelect gOrthogonal[2] = { h, v };
-
- /***********************************|****************************************/
-
- static RgnHandle GetSaveVisRgn(void)
- {
- const long addr = 0x09F2;
-
- return * (RgnHandle *) addr;
- }
-
- /***********************************|****************************************/
-
- inline VHSelect LongerSide(Rect& r)
- {
- if ((r.bottom - r.top) >= (r.left - r.right))
- return v;
- else
- return h;
- }
-
- /***********************************|****************************************/
-
- TLogWindow::TLogWindow ( short windowID ) :
- fWindowResID ( windowID ),
- fDebugWindowPtr ( nil ),
- fHeight ( 14 ),
- fARgn ( nil ),
-
- fRowCount ( 10 ),
-
- fScrollWindowWhenTextIsAdded ( true )
- {
- fSBars [ v ] = fSBars [ h ] = nil;
-
- SetRect(&fStdDrag, 4, 24, qd.screenBits.bounds.right - 4, qd.screenBits.bounds.bottom - 4); //this is suggested in Inside Macintosh
- SetRect(&fStdSize, 20, 20, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom - 20); //arbitrary Minimum size; Maximum size is screen
-
- fViewSize.v = fViewSize.h = 0;
-
- gLogWindowList.Append ( this );
-
- }
-
- /***********************************|****************************************/
-
- TLogWindow::~TLogWindow ( )
- {
- // Close the actual window, if it exists.
- if ( fDebugWindowPtr )
- CloseWindow ( fDebugWindowPtr );
- fDebugWindowPtr = nil;
-
- gLogWindowList.Remove ( this );
- }
-
- /***********************************|****************************************/
-
- Boolean TLogWindow::Initialize ( )
- { CGrafPortSaver saver ( nil );
-
- if ( ! fDebugWindowPtr )
- {
- fDebugWindowPtr = GetNewWindow ( fWindowResID, nil, (WindowPtr) -1 );
-
- FAILNULL ( fDebugWindowPtr );
-
- ((WindowPeek) fDebugWindowPtr)->refCon = (long) this;
-
- fARgn = NewRgn();
-
- //scroll bars
- for ( VHSelect vhs = v; vhs <= h; ++ vhs ) {
- fSBars[vhs] = NewControl(GetWindowPtr(), &GetWindowPtr()->portRect, "\p", false, 0, 0, 1, scrollBarProc, 0);
- (**fSBars[vhs]).contrlRfCon = (long) this;
- }
-
- fScrollOffset.v = fScrollOffset.h = 0;
-
- SetPt(&fViewSize, (2 * kLogWindowHMargin) +
- ( GetRowWidth ( ) ),
- (2 * kLogWindowVMargin) + (fHeight * GetRowCount () ) );
-
- //put the scroll bars in the right place
- Grown();
- UpdateEvent();
-
- //scroll to the end, in case there is some information that needs to be displayed
- SetCtlValue(fSBars[v], LONG_MAX);
- DoScrolling();
- }
-
- return true;
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::WindowFocus(void)
- {
- SetPort( GetWindowPtr() );
- SetOrigin(0, 0);
- ClipRect(&qd.thePort->portRect);
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::ContentFocus(void)
- { Rect r;
-
- SetPort( GetWindowPtr() );
- SetOrigin(fScrollOffset.h, fScrollOffset.v);
- * ( Rect *) &r = * ( Rect *) & qd.thePort->portRect;
- r.right = r.right - 15;
- r.bottom = r.bottom - 15;
- ClipRect(&r);
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::ActivateEvent( short modifiers )
- { Rect r;
- CGrafPortSaver saver ( GetWindowPtr() );
-
- WindowFocus();
-
- r = qd.thePort->portRect;
-
- if ( modifiers & 0x01 )
- {
- ShowControl ( fSBars[v] );
- ShowControl ( fSBars[h] );
- }
- else
- {
- HideControl ( fSBars[v] );
- HideControl ( fSBars[h] );
- }
-
- DrawGrowIcon( GetWindowPtr() );
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::DoScrolling(void)
- { Point newOffset;
- Point delta;
-
- newOffset.v = GetCtlValue(fSBars[v]);
- delta.v = fScrollOffset.v - newOffset.v;
- newOffset.h = GetCtlValue(fSBars[h]);
- delta.h = fScrollOffset.h - newOffset.h;
-
- if ((delta.h != 0) || (delta.v != 0))
- {
- ContentFocus();
-
- ScrollRect(&qd.thePort->portRect, delta.h, delta.v, fARgn);
- fScrollOffset = newOffset;
-
- InvalRgn(fARgn);
-
- UpdateEvent();
- }
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::InsertRow ( unsigned long beforeWhichRow, unsigned long count )
- {
- fRowCount += count ;
- for ( unsigned long index = beforeWhichRow ; index <= GetRowCount(); ++ index )
- InvalidateRow ( index );
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::DeleteRow ( unsigned long whichRow, unsigned long count )
- {
- for ( unsigned long index = whichRow ; index <= GetRowCount (); ++ index )
- InvalidateRow ( index );
- fRowCount -= count ;
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::Draw ( ) const
- {
- if ( GetWindowPtr() )
- Draw ( GetWindowPtr() ->portRect );
- }
-
- /***********************************|****************************************/
-
-
- void TLogWindow::Draw( const Rect& drawRect ) const
- {
- for ( unsigned long index = 1; index <= GetRowCount () ; ++ index )
- DrawRow ( index );
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::DrawRow ( unsigned long whichRow ) const
- {
- MoveTo ( 2, whichRow * fHeight );
- DrawString ( "\pHi Mom!! ");
- }
-
- /***********************************|****************************************/
-
- Rect TLogWindow::GetRectForRow ( unsigned long whichRow ) const
- {
- Rect result;
- result.top = kLogWindowVMargin + ( whichRow - 1 ) * fHeight;
- result.left = kLogWindowHMargin;
- result.bottom = result.top + fHeight;
- result.right = kLogWindowHMargin + GetRowWidth();
-
- return result;
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::ValidateRow ( unsigned long whichRow ) const
- {
- Rect r = GetRectForRow ( whichRow );
- CGrafPortSaver saver ( GetWindowPtr() );
- ValidRect ( &r );
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::InvalidateRow ( unsigned long whichRow ) const
- {
- Rect r = GetRectForRow ( whichRow );
- CGrafPortSaver saver ( GetWindowPtr() );
- InvalRect ( &r );
- }
-
- /***********************************|****************************************/
-
- unsigned long TLogWindow::GetRowCount ( ) const
- {
- return fRowCount;
- }
-
- /***********************************|****************************************/
-
- unsigned long TLogWindow::GetRowWidth ( ) const
- {
- return 200;
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::Grown(void)
- { Rect r;
- ControlHandle anSBar;
- short newMax;
- CGrafPortSaver saver ( GetWindowPtr() );
-
- WindowFocus();
- SetRect (&r, 0, 0, 0, 0 );
- ClipRect(&r);
-
- for (VHSelect vhs = v; vhs <= h; ++vhs )
- {
- anSBar = fSBars[vhs];
-
- r = qd.thePort->portRect;
-
- // Calculate new position of scroll bar
- (( short *) &r.top)[vhs] = (( short *) &r.top)[vhs] - 1;
- (( short *) &r.top)[gOrthogonal[vhs]] = (( short *) &r.bottom)[gOrthogonal[vhs]] - 15;
- (( short *) &r.bottom)[vhs] = (( short *) &r.bottom)[vhs] - 14;
- (( short *) &r.bottom)[gOrthogonal[vhs]] = (( short *) &r.top)[gOrthogonal[vhs]] + 16;
-
- //Move the scroll bar
- MoveControl(anSBar, r.left, r.top);
- SizeControl(anSBar, r.right-r.left, r.bottom-r.top);
-
- if ( vhs == v )
- newMax = fViewSize.v - (r.bottom - r.top);
- else
- newMax = fViewSize.h - (r.right - r.left );
-
- if (newMax < 0)
- newMax = 0;
- SetCtlMax(anSBar, newMax);
- }
-
- InvalidateGrowBox();
-
- DoScrolling(); //in case we are showing too much white space
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::InvalidateGrowBox ( ) const
- { Rect r;
-
- r = qd.thePort->portRect;
- r.top = r.bottom - 15;
- r.left = r.right - 15;
-
- InvalRect(&r);
- }
-
- /***********************************|****************************************/
-
- Boolean GetDefaultWindowInformation ( short dialogID, Boolean& visible, Rect & screenRect );
- Boolean SetDefaultWindowInformation ( DialogPtr dP, short dialogID );
-
- /***********************************|****************************************/
-
- void TLogWindow::MouseDown(short where, Point pt, short modifiers)
- { GrafPtr savePort;
- CGrafPortSaver saver ( GetWindowPtr() );
-
- switch (where) {
- case inDrag:
- DragWindow( GetWindowPtr() , pt, &fStdDrag);
- break;
-
- case inGrow:
- WindowFocus();
-
- long frowResult = GrowWindow( GetWindowPtr() , pt, &fStdSize);
- if (frowResult != 0)
- { short newH = (short) (frowResult & 0xffff );
- short newV = (short) (frowResult >> 16);
-
- InvalidateGrowBox();
- SizeWindow( GetWindowPtr() , newH, newV, true);
- SetDefaultWindowInformation ( GetWindowPtr() , -1 );
-
- Grown();
- }
- break;
-
- case inGoAway:
- if (TrackGoAway( GetWindowPtr() , pt)) {
- SetDefaultWindowInformation ( GetWindowPtr() , -1 );
- HideWindow( GetWindowPtr() );
- }
- break;
-
- case inContent:
- if ( GetWindowPtr() == FrontWindow())
- {
- WindowFocus();
- GlobalToLocal(&pt);
-
- ControlHandle whichControl;
- long partCode = FindControl(pt, GetWindowPtr() , &whichControl);
- switch (partCode)
- {
- case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- pascal void LogWindowTrackControlCallback ( ControlHandle aControl, short partCode );
- partCode = TrackControl(whichControl, pt, NewControlActionProc((ProcPtr) &LogWindowTrackControlCallback));
- DoScrolling();
- WindowFocus();
- break;
-
- case inThumb:
- partCode = TrackControl(whichControl, pt, NULL);
- DoScrolling();
- break;
-
- case 0:
- break;
- }
- }
- else
- SelectWindow( GetWindowPtr() );
-
- break;
- }
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::Scroll ( short howManyLines )
- { CGrafPortSaver saver ( GetWindowPtr() );
-
- short val = GetCtlValue(fSBars[v]);
- if (((howManyLines < 0) && (val > GetCtlMin(fSBars[v]))) ||
- ((howManyLines > 0) && (val < GetCtlMax(fSBars[v]))))
- {
- SetCtlValue(fSBars[v], val + howManyLines * fHeight);
- DoScrolling();
- }
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::ShowPoint(Point pt)
- { Point minToSee;
- short deltaCd;
-
- if ( GetWindowPtr() != NULL)
- {
- WindowFocus();
-
- SetPt(&minToSee, 50, fHeight);
-
- // the following code is actually better than writing a loop with VHSelect
- deltaCd = pt.v + minToSee.v - (qd.thePort->portRect.bottom - 15 + fScrollOffset.v);
- if (deltaCd <= 0)
- {
- deltaCd = pt.v - minToSee.v - (qd.thePort->portRect.top + fScrollOffset.v);
- if (deltaCd >= 0)
- deltaCd = 0;
- }
- SetCtlValue(fSBars[v], GetCtlValue(fSBars[v]) + deltaCd);
-
- deltaCd = pt.h + minToSee.h - (qd.thePort->portRect.right - 15 + fScrollOffset.h);
- if (deltaCd <= 0)
- {
- deltaCd = pt.h - minToSee.h - (qd.thePort->portRect.left + fScrollOffset.h);
- if (deltaCd >= 0)
- deltaCd = 0;
- }
- SetCtlValue(fSBars[h], GetCtlValue(fSBars[h]) + deltaCd);
-
- DoScrolling();
- }
- }
-
- /***********************************|****************************************/
-
- pascal void LogWindowTrackControlCallback ( ControlHandle aControl, short partCode )
- {
- if (partCode != 0)
- {
- TLogWindow* logWindow = (TLogWindow *) (**aControl).contrlRfCon;
-
- Boolean up = (partCode == inUpButton) || (partCode == inPageUp);
- short ctlValue = GetCtlValue(aControl);
-
- // Avoid flicker in setting thumb, if (user tries to scroll past end
- if ((up && (ctlValue > GetCtlMin(aControl))) ||
- (!up && (ctlValue < GetCtlMax(aControl))))
- {
- Rect r = (*aControl)->contrlRect; // heap may compact when we call LongerSide
- VHSelect vhs = LongerSide(r); // this tells us which way we are scrolling
-
- short delta = 10;
-
- if ( logWindow )
- { WindowPtr ourWindow = logWindow->GetWindowPtr();
-
- if ((partCode == inPageUp) || (partCode == inPageDown) && ourWindow )
- if ( vhs == v )
- delta = ourWindow->portRect.bottom - ourWindow->portRect.top;
- else
- delta = ourWindow->portRect.right - ourWindow->portRect.left;
- else
- delta = logWindow->fHeight;
- }
-
- if (up)
- delta = - delta;
-
- SetCtlValue(aControl, ctlValue + delta);
-
- if ( logWindow )
- {
- logWindow->DoScrolling();
- logWindow->WindowFocus();
- }
- }
- }
- }
-
- /***********************************|****************************************/
-
- static Rect GetRegionBoundingRect ( RgnHandle rgn )
- {
- if ( rgn )
- return ( ** rgn ).rgnBBox;
- Rect r = { -32767, -32767, 32767, 32767 };
- return r;
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::UpdateEvent(void)
- { GrafPtr savePort;
- RgnHandle saveSaveVisRgn;
- RgnHandle saveVisRgn;
-
- if (( GetWindowPtr() != NULL) &&
- (!EmptyRgn( ((WindowPeek) GetWindowPtr() )->port.visRgn)))
- { CGrafPortSaver saver ( GetWindowPtr() );
-
- saveSaveVisRgn = NewRgn();
- saveVisRgn = GetSaveVisRgn();
-
- CopyRgn(saveVisRgn, saveSaveVisRgn);
-
- BeginUpdate( GetWindowPtr() );
-
- WindowFocus();
-
- EraseRect(&qd.thePort->portRect);
-
- DrawGrowIcon( GetWindowPtr() );
- DrawControls( GetWindowPtr() );
-
- ContentFocus();
-
- Rect updateRect = GetRegionBoundingRect ( GetWindowPtr() ->visRgn );
-
- Draw( );
-
- EndUpdate( GetWindowPtr() );
-
- CopyRgn(saveSaveVisRgn, saveVisRgn);
- DisposeRgn(saveSaveVisRgn);
- }
- }
-
- /***********************************|****************************************/
-
- WindowPtr TLogWindow::GetWindowPtr ( ) const
- {
- if ( ! fDebugWindowPtr )
- ((TLogWindow *) this )->Initialize () ;
-
- return fDebugWindowPtr ;
- }
-
- /***********************************|****************************************/
-
- static TLogWindow* FindLogWindow ( WindowPtr wP )
- {
- for ( unsigned long index = 1; index <= gLogWindowList.Count (); ++ index )
- if ( gLogWindowList.Get ( index )->GetWindowPtr() == wP )
- return gLogWindowList.Get ( index );
-
- return nil;
- }
-
- /***********************************|****************************************/
-
- /*
- -- Paul's Writeln routines ----------------------------------------------------------------
- */
- Boolean TLogWindow::IsLogWindowEvent (const EventRecord& event )
- {
- Boolean result = false;
-
- switch ( event.what )
- {
- case mouseDown:
- {
- { WindowPtr WindowPointedTo; //window where the mouse is
- Point MouseLoc = event.where;
-
- short WindoPart = FindWindow(MouseLoc, &WindowPointedTo);
- TLogWindow* logWindow = FindLogWindow ( WindowPointedTo );
-
- if ( logWindow )
- {
- if (WindowPointedTo != FrontWindow())
- SelectWindow(WindowPointedTo);
- else
- logWindow->MouseDown(WindoPart, MouseLoc, event.modifiers);
- result = true;
- }
- }
- break;
- }
-
- case activateEvt:
- { TLogWindow* logWindow = FindLogWindow ( WindowPtr(event.message) );
- if ( logWindow )
- {
- logWindow->ActivateEvent ( event.modifiers );
- result = true;
- }
- break;
- }
-
- case updateEvt:
- { TLogWindow* logWindow = FindLogWindow ( WindowPtr( event.message ) );
- if ( logWindow )
- {
- logWindow->UpdateEvent();
- result = true;
- }
- break;
- }
- }
-
- return result;
-
- } //Event
-
- /***********************************|****************************************/
-
- void TLogWindow::Show (void) const
- {
- if ( ! GetWindowPtr() )
- ((TLogWindow *)this )->Initialize ( );
-
- SetDefaultWindowInformation ( GetWindowPtr() , -1 );
-
- ShowWindow ( GetWindowPtr() );
- }
-
- /***********************************|****************************************/
-
- void TLogWindow::Hide (void) const
- {
- if ( GetWindowPtr() )
- {
- SetDefaultWindowInformation ( GetWindowPtr() , -1 );
-
- HideWindow ( GetWindowPtr() );
- }
- }
-
- /***********************************|****************************************/
-
- void DrawString ( short x, short y, TRString s )
- {
- MoveTo ( x, y );
- if ( s.GetScript () == smRoman )
- DrawText ( (Ptr) (const char *) s , 0, s.GetLength() );
- }
-
- /***********************************|****************************************/
-
- /***********************************|****************************************/
-
- void DrawString ( short x, short y, StringPtr s )
- {
- MoveTo ( x, y );
- DrawString ( s );
- }
-
- /***********************************|****************************************/
-
- void DrawNumber ( short x, short y, long n )
- { char buffer[16];
- sprintf ( buffer, "%d", n );
-
- MoveTo ( x, y );
- DrawText ( & buffer, 0, strlen ( buffer ) );
- }
-
- /***********************************|****************************************/
-
- void EraseLineTo ( short x, short y )
- {
- Point pt;
- GetPen ( &pt );
-
- Rect r;
- r.top = y - 10;
- r.bottom = y + 2;
- r.left = pt.h;
- r.right = x;
-
- EraseRect ( &r );
- }
-
- /***********************************|****************************************/
-
- void EraseLineToDrawString ( short x, short y, StringPtr s )
- {
- EraseLineTo ( x, y );
- DrawString ( x, y, s );
- }
-
- /***********************************|****************************************/
-
- void EraseLineToDrawNumber ( short x, short y, long n )
- {
- EraseLineTo ( x, y );
- DrawNumber ( x, y, n );
- }
-
-